package com.dykj.fireFighting.web.xfyl;

import com.dykj.fireFighting.api.socialUnit.xfyl.dto.UnitSafeTrainPlanDto;
import com.dykj.fireFighting.api.socialUnit.xfyl.entity.UnitSafeDrillPlan;
import com.dykj.fireFighting.api.socialUnit.xfyl.entity.UnitSafeTrainPlan;
import com.dykj.fireFighting.api.socialUnit.xfyl.service.IUnitSafeTrainPlanService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 消防安全培训计划接口
 *
 * @Author LJJ
 * @Date 2021-06-15 16:03:51
 */
@Slf4j
@Api(value = "消防安全培训计划接口", tags = {"消防安全培训计划接口"})
@RestController
@RequestMapping("/unitSafeTrainPlan")
public class UnitSafeTrainPlanController {

    @DubboReference
    private IUnitSafeTrainPlanService unitSafeTrainPlanService;

    /**
     * 获取单位id
     * @return String
     */
    public String getEnterpriseId() {
        try {
            return Long.toString(ShiroUtil.getUser().getEnterpriseId());
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 获取用户名称
     * @return String
     */
    public String getName() {
        try {
            return ShiroUtil.getUser().getName();
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }


    /**
     * 消防安全培训计划列表
     *
     * @return
     */
    @AutoLog(value = "消防安全培训计划-分页列表查询")
    @ApiOperation(value = "消防安全培训计划分页列表", notes = "消防安全培训计划分页列表")
    @GetMapping("/list")
    @RequiresPermissions("sms/unitSafeTrainPlan/list")
    public CommonResult<PageVo<UnitSafeTrainPlan>> treeGrid(UnitSafeTrainPlanDto commonDto) {
        if (StringUtils.isBlank(commonDto.getUnitId())) {
            if (StringUtils.isBlank(getEnterpriseId())) {
                throw new BaseException("请登录系统");
            }
            commonDto.setUnitId(getEnterpriseId());
        }
        PageVo<UnitSafeTrainPlan> page = unitSafeTrainPlanService.pageList(commonDto);
        return CommonResult.success(page);
    }

    /**
     * 获取消防安全培训计划信息
     */
    @AutoLog(value = "消防安全培训计划-获取消防安全培训计划信息")
    @ApiOperation(value = "获取消防安全培训计划信息", notes = "获取消防安全培训计划信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    @RequiresPermissions("sms/unitSafeTrainPlan/info")
    public CommonResult<UnitSafeTrainPlan> info(@PathVariable("id") String id) {
        UnitSafeTrainPlan unitSafeTrainPlan = unitSafeTrainPlanService.getById(id);
        return CommonResult.success(unitSafeTrainPlan);
    }

    /**
     * 保存消防安全培训计划信息
     */
    @AutoLog(value = "消防安全培训计划-保存消防安全培训计划信息")
    @ApiOperation(value = "保存消防安全培训计划信息", notes = "保存消防安全培训计划信息")
    @PostMapping("/save")
    @RequiresPermissions("sms/unitSafeTrainPlan/save")
    public CommonResult save(@Valid @RequestBody UnitSafeTrainPlan unitSafeTrainPlan) {
        try {
            unitSafeTrainPlan.setCreateTime(new Date());
            unitSafeTrainPlan.setUpdateTime(new Date());
            if (StringUtils.isBlank(getEnterpriseId())) {
                throw new BaseException("请登录系统");
            } else {
                unitSafeTrainPlan.setUnitId(getEnterpriseId());
                unitSafeTrainPlan.setCreateBy(getName());
            }
            unitSafeTrainPlanService.save(unitSafeTrainPlan);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException("运行异常，请联系管理员");
        }
    }

    /**
     * 修改消防安全培训计划信息
     */
    @AutoLog(value = "消防安全培训计划-修改消防安全培训计划信息")
    @ApiOperation(value = "修改消防安全培训计划信息", notes = "修改消防安全培训计划信息")
    @PostMapping("/update")
    @RequiresPermissions("sms/unitSafeTrainPlan/update")
    public CommonResult update(@Valid @RequestBody UnitSafeTrainPlan unitSafeTrainPlan) {
        try {
            unitSafeTrainPlan.setUpdateTime(new Date());
            unitSafeTrainPlan.setUpdateBy(getName());
            unitSafeTrainPlanService.updateById(unitSafeTrainPlan);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException("运行异常，请联系管理员");
        }
    }

    /**
     * 删除消防安全培训计划信息
     */
    @AutoLog(value = "消防安全培训计划-删除消防安全培训计划信息")
    @ApiOperation(value = "删除消防安全培训计划信息", notes = "删除消防安全培训计划信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    @RequiresPermissions("sms/unitSafeTrainPlan/delete")
    public CommonResult delete(@RequestBody String[] ids) {
        try {
            unitSafeTrainPlanService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException("运行异常，请联系管理员");
        }
    }

    @AutoLog(value = "消防安全培训计划-所有消防安全培训计划下拉")
    @ApiOperation(value = "消防安全培训计划下拉", notes = "消防安全培训计划下拉")
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public CommonResult<List<UnitSafeTrainPlan>> query() {
        List<UnitSafeTrainPlan> list = unitSafeTrainPlanService.queryList(getEnterpriseId());
        return CommonResult.success(list);
    }
}
